<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace api\home\controller;

use cmf\controller\RestBaseController;
use think\Db;
use think\Validate;
use app\portal\model\PortalPostModel;
/**
 * @title 产品系列
 */
class SeriesController extends RestBaseController
{

    /**
     * @title 产品系列分类
     * @description 接口说明
     * @author 开发者
     * @url /api/home/series/seriesType
     * @method GET
     *
     * @param name:language type:string require:0 default: other desc:语言切换（英文传递此字段（en）,中文无需传递）
     *
     * @return data:一级分类@
     * @data id:一级分类id thumbnail:图片路径 name:一级分类名称 children:子类@
     * @children id:二级分类id thumbnail:图片路径 name:二级分类名称
     */
    public function seriesType(){
        $language = $this->request->param('language');

        if(isset($language) && !empty($language)){
            //英文
            $field_type = 'id,pid,thumbnail,name_en name';
        }else{
            //中文
            $field_type = 'id,pid,thumbnail,name';
        }
        $parent_res = CommonController::findSeriesType($field_type);
        $this->success('成功',['data'=>$parent_res]);
    }

    /**
     * @title 产品系列列表（一级分类查找）
     * @description 接口说明
     * @author 开发者
     * @url /api/home/series/seriesListByParent
     * @method GET
     *
     * @param name:language type:string require:0 default: other desc:语言切换（英文传递此字段（en）,中文无需传递）
     * @param name:parent_id type:inter require:1 default: other desc:一级分类id
     * @param name:page type:inter require:1 default: other desc:分页页码
     *
     * @return data:产品系列列表@
     * @data id:产品系列id thumbnail:图片路径 title:系列名称
     *
     * @return count_page:总页码
     */
    public function seriesListByParent(){
        $language = $this->request->param('language');
        $parent_id = $this->request->param('parent_id');//二级分类id
        $page = $this->request->param('page');//分页
        //验证
        $rule = config('site.parent_id');
        $validate = new Validate($rule['rule'],$rule['msg']);
        if (!$validate->check(['parent_id'=>$parent_id,'page'=>$page])) {
            $this->error($validate->getError());
        }

        if(isset($language) && !empty($language)){
            //英文
            $field_series = 'id,thumbnail,title_en title';
        }else{
            //中文
            $field_series = 'id,thumbnail,title';
        }
        //查找二级分类
        $type = CommonController::selectData('type',['pid'=>$parent_id],'id');
        $children_ids = array_column($type,'id');
        $limit = CommonController::series_limit;
        $data = Db::name('series')
            ->whereIn('t_id',$children_ids)
            ->field($field_series)
            ->page($page,$limit)
            ->order('id desc')
            ->select()
            ->toArray();
        $count = Db::name('series')->whereIn('t_id',$children_ids)->count();
        $count_page = ceil($count/$limit);
        $this->success('成功',['data'=>$data,'count_page'=>$count_page]);
    }

    /**
     * @title 产品系列列表（二级分类查找）
     * @description 接口说明
     * @author 开发者
     * @url /api/home/series/seriesListBySon
     * @method GET
     *
     * @param name:language type:string require:0 default: other desc:语言切换（英文传递此字段（en）,中文无需传递）
     * @param name:children_id type:inter require:1 default: other desc:二级分类id
     * @param name:page type:inter require:1 default: other desc:分页页码
     *
     * @return data:产品系列列表@
     * @data id:产品系列id thumbnail:图片路径 title:系列名称
     *
     * @return count_page:总页码
     */
    public function seriesListBySon(){
        $language = $this->request->param('language');
        $children_id = $this->request->param('children_id');//二级分类id
        $page = $this->request->param('page');//分页
        //验证
        $rule = config('site.children_id');
        $validate = new Validate($rule['rule'],$rule['msg']);
        if (!$validate->check(['children_id'=>$children_id,'page'=>$page])) {
            $this->error($validate->getError());
        }

        if(isset($language) && !empty($language)){
            //英文
            $field_series = 'id,thumbnail,title_en title';
        }else{
            //中文
            $field_series = 'id,thumbnail,title';
        }
        $limit = CommonController::series_limit;
        $data =  Db::name('series')
            ->where(['t_id'=>$children_id])
            ->field($field_series)
            ->page($page,$limit)
            ->order('id desc')
            ->select()
            ->toArray();
        $count = Db::name('series')->where(['t_id'=>$children_id])->count();
        $count_page = ceil($count/$limit);
        $this->success('成功',['data'=>$data,'count_page'=>$count_page]);
    }

    /**
     * @title 产品系列详情
     * @description 接口说明
     * @author 开发者
     * @url /api/home/series/seriesDetail
     * @method GET
     *
     * @param name:language type:string require:0 default: other desc:语言切换（英文传递此字段（en）,中文无需传递）
     * @param name:series_id type:inter require:1 default: other desc:产品系列id
     *
     * @return data:产品系列列表@
     * @data id:产品系列id thumbnail:列表图片路径（不使用了） thumbnail1:详情图片路径 thumbnail2:详情放大图片路径 title:系列名称 detail:详情（富文本） hotline:电话咨询
     *
     * @return parent_name:一级分类名称
     */
    public function seriesDetail(){
        $language = $this->request->param('language');
        $series_id = $this->request->param('series_id');//列表id
        //验证
        $rule = config('site.series_id');
        $validate = new Validate($rule['rule'],$rule['msg']);
        if (!$validate->check(['series_id'=>$series_id])) {
            $this->error($validate->getError());
        }
        if(isset($language) && !empty($language)){
            //英文
            $field_series = 'id,t_id,thumbnail,thumbnail1,thumbnail2,title_en title,detail_en detail,hotline';
            $flag = 'name_en name';
        }else{
            //中文
            $field_series = 'id,t_id,thumbnail,thumbnail1,thumbnail2,title,detail,hotline';
            $flag = 'name';
        }
        $res = CommonController::findData('series',['id'=>$series_id],$field_series);
        //查找父类名称
        $pid = CommonController::findData('type',['id'=>$res['t_id']],'pid');
        $parent_name = CommonController::findData('type',['id'=>$pid['pid']],$flag);
        if($res){
            $contentModel = new PortalPostModel();
            $res['detail'] = $contentModel->getPostContentAttr($res['detail']);
        }
        $this->success('成功',['data'=>$res,'parent_name'=>$parent_name['name']]);
    }
}
